/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.dat076.datesite.controllers;

import edu.chl.dat076.datesite.ejbs.NonexistentEntityException;
import edu.chl.dat076.datesite.ejbs.UserEJBLocal;
import edu.chl.dat076.datesite.entities.SiteUser;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Mikey
 */
@ManagedBean
@ViewScoped
public class DateProfileBackingBean implements Serializable {
    
    @EJB
    private UserEJBLocal userJPACtrl;
  
    private SiteUser user;
    
    private SiteUser me;
    
    private boolean favorite;
    
    @PostConstruct
    public void postConstruct () {
        setUser((SiteUser) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("userProfile"));
        setMe((SiteUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user"));
        setFavorite();
    }
    
    public SiteUser getUser () {
        return user;
    }
    
    public void setUser (SiteUser user) {
        this.user = user;
    }

    public void setFavorite() {
        favorite = me.isFriends(user);
    }
    
    public boolean getFavorite() {
        return favorite;
    }

    public SiteUser getMe() {
        return me;
    }

    public void setMe(SiteUser me) {
        this.me = me;
    }
    
    public String sendMessage() {
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mFromSite", "profile");
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put("receiverUser", user.getUsername());
        return "conversation.jsf?faces-redirect=true";
    }
    
    public String addFriend() throws NonexistentEntityException {        
        me.addFriend(user);
        userJPACtrl.editUser(me);
        return "gotoFavorites";
    }
}